import redis
import json
import pandas as pd
import jieba
import datetime
from collections import Counter
import thulac

r = redis.Redis(decode_responses=True)
comments = list(r.hgetall('ychx').values())
title = [v for c in comments for t, v in json.loads(c).items() if t == 'title']
votes = [int(v) for c in comments for t, v in json.loads(c).items() if t == 'votes']
allstar = [v for c in comments for t, v in json.loads(c).items() if t == 'allstar']
time = [datetime.datetime.strptime(v, '%Y-%m-%d') for c in comments for t, v in json.loads(c).items() if t == 'time']
short = [v for c in comments for t, v in json.loads(c).items() if t == 'short']
user_name = [v for c in comments for t, v in json.loads(c).items() if t == 'user_name']
columns = ['votes', 'allstar', 'title']
data = {'votes': votes, 'allstar': allstar, 'title': title}
df = pd.DataFrame(index=time, columns=columns, data=data)
# 基本信息
print(df.describe())
# 前十有用
df.sort_values('votes', ascending=False, inplace=True)
print(df.head(10))
# fenci = jieba.lcut(''.join(short), cut_all=True)
# 设置字典
# jieba.set_dictionary('')
fenci = jieba.lcut(''.join(short))
fenci = Counter(fenci)
print(fenci.most_common(50))
